//
//  ModifyPassWordViewController.m
//  PigMarket
//
//  Created by 张敬 on 2019/8/9.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "ModifyPassWordViewController.h"

@interface ModifyPassWordViewController ()

@property (nonatomic, strong) UILabel *label;

@property (nonatomic, strong) UIButton *button;

@property (nonatomic, strong) UITextField *oldpasswordTextField;
@property (nonatomic, strong) UITextField *newpasswordTextField;
@property (nonatomic, strong) UITextField *againpasswordTextField;

@property (nonatomic, strong) UIImageView *oldIcon;
@property (nonatomic, strong) UIImageView *newsIcon;
@property (nonatomic, strong) UIImageView *againIcon;

@property (nonatomic, copy) NSString *oldpassword;
@property (nonatomic, copy) NSString *newpassword;
@property (nonatomic, copy) NSString *againpassword;



@end

@implementation ModifyPassWordViewController

#pragma mark - lifecycle methods

- (void)viewDidLoad{
    
    [super viewDidLoad];
    
    self.baseNavView.line.hidden = YES;
    self.view.backgroundColor = KWhiteColor;
    
    [self.view addSubview:self.label];
    
    [self.view addSubview:self.oldpasswordTextField];
    [self.view addSubview:self.newpasswordTextField];
    [self.view addSubview:self.againpasswordTextField];
    
    [self.view addSubview:self.oldIcon];
    [self.view addSubview:self.newsIcon];
    [self.view addSubview:self.againIcon];
    
    [self.view addSubview:self.button];
    
    [self setupLayout];

}

- (void)setupLayout
{
    {
        _label.size = CGSizeMake(KScreenWidth - 60, 30);
        _label.mj_y = self.baseNavView.bottom + 36;
        _label.mj_x = 30;
    }
    
    {
        _oldpasswordTextField.size = _newpasswordTextField.size = _againpasswordTextField.size
        = CGSizeMake(KScreenWidth - 80, 54);
        _oldpasswordTextField.mj_x = _newpasswordTextField.mj_x = _againpasswordTextField.mj_x = 30;
        _oldpasswordTextField.mj_y = _label.bottom + 50;
        _newpasswordTextField.mj_y = _oldpasswordTextField.bottom + 14;
        _againpasswordTextField.mj_y = _newpasswordTextField.bottom + 14;
    }
    
    {
        _oldIcon.size = _newsIcon.size = _againIcon.size = CGSizeMake(54, 54);
        _oldIcon.mj_x = _newsIcon.mj_x = _againIcon.mj_x = KScreenWidth - 64;
        _oldIcon.mj_y = _oldpasswordTextField.mj_y;
        _newsIcon.mj_y = _newpasswordTextField.mj_y;
        _againIcon.mj_y = _againpasswordTextField.mj_y;
    }
    
    {
        CGFloat h = _againpasswordTextField.height + 14;
        for (NSInteger k = 0; k <3; k++) {
            CAShapeLayer *line = [CAShapeLayer layer];
            line.fillColor = mIconLineColor.CGColor;
            
            UIBezierPath *path = [UIBezierPath bezierPathWithRect:
                                  CGRectMake(30, _oldpasswordTextField.bottom + h *k, KScreenWidth - 60, 1)];
            line.path = path.CGPath;
            [self.view.layer addSublayer:line];
        }
    }
    
    {
        _button.size = CGSizeMake(KScreenWidth - 60, 48);
        _button.mj_x = 30;
        _button.mj_y = _againpasswordTextField.bottom + 40;
    }
}



#pragma mark - networking methods

#pragma mark - setter methods


#pragma mark - getter methods

- (UILabel *)label{
    
    if (!_label) {
        _label = [UILabel makeLabelWithText:@"修改密码" textColor:UIColorFromRGB(0x363636) font:mFont(22) textAliment:0];
    }
    return _label;
}


- (UIButton *)button{
    
    if (!_button) {
        _button = [UIButton makeButtonWithText:@"确定" textColor:KWhiteColor font:mMediumFont(16) textAliment:1 cornerRadius:5];
        [_button setBackgroundImage:[UIImage imageNamed:@"meat_shop_sumbit_bg"] forState:UIControlStateNormal];
        
        @weakify(self);
        _button.tapBlock = ^{
            @strongify(self);
            
            if (self.oldpassword.length <= 0) {
                
                [self showHudWithMsg:@"请输入原密码"];
                return ;
            }
            
            if (self.newpassword.length <= 0) {
               
                [self showHudWithMsg:@"请输入6-20位新密码"];
                return ;
            }
            
            if ([self.newpassword isEqualToString:self.oldpassword]){
                
                [self showHudWithMsg:@"请输入正确的新密码"];
                return ;
            }
            
            
            if (self.newpassword.length <6 || self.newpassword.length >20) {
                [self showHudWithMsg:@"请输入6-20位新密码"];
                return ;
            }
           
            if (self.againpassword.length <= 0) {
                
                [self showHudWithMsg:@"再次输入密码"];
                return ;
            }
            
            if (![self.newpassword isEqualToString:self.againpassword]){
                
                [self showHudWithMsg:@"请再次输入正确的新密码"];
                return ;
            }
            NSLog(@"修改密码正确");
        };

    }
    return _button;
}


- (UITextField *)oldpasswordTextField{
    
    if (!_oldpasswordTextField) {
        _oldpasswordTextField = [[UITextField alloc]init];
        _oldpasswordTextField.placeholder = @"请输入原密码";
        _oldpasswordTextField.textColor = UIColorFromRGB(0x363636);
        _oldpasswordTextField.font = mFont(16);
        _oldpasswordTextField.secureTextEntry = YES;
        _oldpasswordTextField.keyboardType = UIKeyboardTypeDefault;
        _oldpasswordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        //_oldpasswordTextField.delegate = self;
        _oldpasswordTextField.returnKeyType = UIReturnKeyDone;
    }
    return _oldpasswordTextField;
}


- (UITextField *)newpasswordTextField{
    
    if (!_newpasswordTextField) {
        _newpasswordTextField = [[UITextField alloc]init];
        _newpasswordTextField.textColor = UIColorFromRGB(0x363636);
        _newpasswordTextField.placeholder = @"请输入6-20位新密码";
        _newpasswordTextField.font = mFont(16);
        _newpasswordTextField.keyboardType = UIKeyboardTypeDefault;
        _newpasswordTextField.secureTextEntry = YES;
        _newpasswordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _newpasswordTextField.returnKeyType = UIReturnKeyDone;
        //_newpasswordTextField.delegate = self;
    }
    return _newpasswordTextField;
}


- (UITextField *)againpasswordTextField{
    
    if (!_againpasswordTextField) {
        _againpasswordTextField = [[UITextField alloc]init];
        _againpasswordTextField.placeholder = @"再次输入密码";
        _againpasswordTextField.textColor = UIColorFromRGB(0x363636);
        _againpasswordTextField.font = mFont(16);
        _againpasswordTextField.secureTextEntry = YES;
        _againpasswordTextField.keyboardType = UIKeyboardTypeDefault;
        _againpasswordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        //_againpasswordTextField.delegate = self;
        _againpasswordTextField.returnKeyType = UIReturnKeyDone;
    }
    return _againpasswordTextField;
}


- (UIImageView *)oldIcon{
    
    if (!_oldIcon) {
        _oldIcon = [UIImageView makeImageByImageName:@"login_hidden" cornerRadius:0];
        _oldIcon.isNeedAnimation = YES;
        __block BOOL isClose = NO;
        @weakify(self);
        _oldIcon.tapBlock = ^{
            @strongify(self);
            isClose = !isClose;
            self.oldIcon.image = isClose?[UIImage imageNamed:@"login_show"] :[UIImage imageNamed:@"login_hidden"];
            self.oldpasswordTextField.secureTextEntry = !isClose;
        };
    }
    return _oldIcon;
}


- (UIImageView *)newsIcon{
    
    if (!_newsIcon) {
        _newsIcon = [UIImageView makeImageByImageName:@"login_hidden" cornerRadius:0];
        _newsIcon.isNeedAnimation = YES;
        __block BOOL isClose = NO;
        @weakify(self);
        _newsIcon.tapBlock = ^{
            @strongify(self);
            isClose = !isClose;
            self.newsIcon.image = isClose?[UIImage imageNamed:@"login_show"] :[UIImage imageNamed:@"login_hidden"];
            self.newpasswordTextField.secureTextEntry = !isClose;
        };
    }
    return _newsIcon;
}


- (UIImageView *)againIcon{
    
    if (!_againIcon) {
        _againIcon = [UIImageView makeImageByImageName:@"login_hidden" cornerRadius:0];
        _againIcon.isNeedAnimation = YES;
        __block BOOL isClose = NO;
        @weakify(self);
        _againIcon.tapBlock = ^{
            @strongify(self);
            isClose = !isClose;
            self.againIcon.image = isClose?[UIImage imageNamed:@"login_show"] :[UIImage imageNamed:@"login_hidden"];
            self.againpasswordTextField.secureTextEntry = !isClose;
        };

    }
    return _againIcon;
}


#pragma mark - other methods

- (void)textFieldDidChanged:(NSNotification *)notification
{
    UITextField *textField = notification.object;
   // NSLog(@" !!!!!   %@",textField.text);
    
    if (textField == self.oldpasswordTextField) {
        self.oldpassword = textField.text;
    }else if (textField == self.newpasswordTextField){
        self.newpassword = textField.text;
    }else if (textField == self.againpasswordTextField){
        self.againpassword = textField.text;
    }
}

//取消键盘响应
- (void)hideKeyboard{
    
    [self.view endEditing:YES];
}


- (void)addNotifications{
    
    [kNotificationCenter addObserver:self selector:@selector(textFieldDidChanged:) name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)removeNotifications{
    
    [kNotificationCenter removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}


@end
